<template>
  <div class="container-box">
    <p>停车管理：车辆使用管理</p>
    <!--筛选表单-->
    <div class="filter-form">
      <el-form label-width="120px" :inline="true" size="small" label-position="top">
        <el-form-item prop="community_code">
          <div>小区名称</div>
          <SelectView v-model="queryData.community_code" />
        </el-form-item>
        <el-form-item>
          <div>车位名称</div>
          <el-input v-model="queryData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <div>创建时间</div>
          <el-date-picker
            v-model="queryData.create_time"
            style="width:200px"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item lass="btn">
          <div>操作</div>
          <el-button @click="onSearchCancel">重置</el-button>
          <el-button type="primary" @click="onSearchSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--操作区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div>
        <el-button type="primary" size="small" plain @click="dialogShow = true">新增通知公告</el-button>
      </div>
    </el-row>
    <!--表单区域-->
    <div class="table-box">
      <el-table :data="list" border stripe>
        <!-- prop表示这一列展示哪个数据 -->
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="community.name" label="小区名称" />
        <el-table-column prop="name" label="车位名称" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="{row}">
            <span>{{ moment(row.create_time) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{row}">
            <el-button size="small" type="primary" icon="el-icon-edit" circle @click="editRow(row.id)" />
            <el-popconfirm title="确定删除吗？" @confirm="delRow(row.id)">
              <el-button slot="reference" size="small" type="danger" icon="el-icon-delete" circle />
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页区域-->
    <el-row type="flex" justify="space-between" class="tool-bar">
      <div />
      <div class="page">
        <el-pagination
          :current-page.sync="queryData.current"
          :page-sizes="[4, 8, 10, 20]"
          :page-size="queryData.pageSize"
          layout="total, sizes, prev, pager,next,jumper"
          :total="total"
          background
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-row>
    <!-- 弹框区域 -->
    <el-drawer title="车位使用信息" size="40%" :visible.sync="dialogShow">
      <div class="form_flex">
        <el-form ref="form1" :model="formData" :rules="formRules" label-width="120px" inline size="normal">
          <el-row>
            <el-form-item label="所属小区" prop="community_code">
              <SelectView v-model="formData.community_code" />
            </el-form-item>
            <el-form-item label="车位编号" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" type="textarea" />
            </el-form-item>
          </el-row>
          <el-row type="flex" justify="end">
            <el-button size="small" type="primary" @click="btnOk">提交</el-button>
            <el-button size="small" @click="dialogShow = false">取消</el-button>
          </el-row>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { addUserApi, delUsageApi, editUserApi, getIndexListApi, editIndexId } from '@/api/index_car'
import moment from 'moment'
import SelectView from '../components/selectView.vue'
export default {
  name: 'IndexCar',
  components: { SelectView },
  data() {
    return {
      // value: '',
      list: [],
      formData: {
        name: '',
        community_code: '',
        remark: ''
      },
      queryData: {
        community_code: '',
        name: '',
        current: 1,
        pageSize: 5,
        create_time: '',
        remark: ''
      },
      formRules: {
        community_code: [{
          required: true,
          message: '11',
          targger: 'blur'
        }],
        name: [{
          required: true,
          message: '请填写',
          targger: 'blur'
        }],
        remark: [{
          required: true,
          message: '请填写',
          targger: 'blur'
        }]
      },
      total: 0,
      options: [],
      dialogShow: false,
      msg: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // 获取表单数据
    async initData() {
      const res = await getIndexListApi(this.queryData)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.count
      this.msg = res.msg
    },
    type() { },
    // 删除
    async delRow(id) {
      await delUsageApi(id)
      this.$message.success(this.msg)
      if (this.queryData.current > 1 && this.list.length === 1) {
        this.queryData.current--
      }
      this.initData()
    },
    // 提交
    async btnOk() {
      // await this.$refs.form1.validate()
      this.formData.id ? await editUserApi(this.formData) : await addUserApi(this.formData)
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      this.dialogShow = false
      this.initData()
      this.formData = {
        name: '',
        community_code: '',
        remark: ''
      }
      this.$refs.form1.clearValidate()
    },
    // 分页
    sizeChange(val) {
      this.queryData.pageSize = val
      this.initData()
    },
    currentChange(val) {
      this.queryData.current = val
      this.initData()
    },
    // 重置
    onSearchCancel() {
      this.queryData = {
        community_code: '',
        name: '',
        current: 1,
        pageSize: 5,
        create_time: ''
      }
      this.initData()
    },
    // 查询
    onSearchSubmit() {
      this.queryData.current = 1
      this.initData()
    },
    // 关闭
    btnCancel() {
      // console.log(111);
      this.formData = {
        name: '',
        community_code: '',
        remark: ''
      }
    },
    // 回显
    async editRow(id) {
      // this.$refs.form.validate()
      const res = await editIndexId(id)
      // console.log(res);
      this.formData = res.data
      this.dialogShow = true
    },
    // 时间转化
    moment(time) {
      return moment(time).format('YYYY/MM/DD hh:mm:ss')
    }
  }
}
</script>
<style scoped>
.form_flex .el-row {
  display: flex;
}

.container-box {
  padding: 30px;
}

.filter-form,
.tool-bar {
  border: 1px solid #ddd;
  padding: 15px;
  margin-bottom: 10px;
}

.filter-form .el-form-item {
  width: 15%;
}

.el-input {
  width: 200px;
}

.el-select {
  width: 200px;
}
.btn{
  margin-left: 250px;
}
.el-drawer__title{
  padding: 0 30px;
}
</style>
